package zero.tech.games.log.hot;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import zero.tech.core.data.PageBean;
import zero.tech.games.common.RedisConsts;
import zero.tech.games.entity.log.LogSafetyChange;
import zero.tech.games.log.mapper.LogSafetyChangeMapper;
import zero.tech.redis.cache.RedisUtil;

@Component
public class LogSafetyChangeUtil {
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private LogSafetyChangeMapper logSafetyChangeMapper;
	
	@SuppressWarnings("unchecked")
	public PageBean<LogSafetyChange> listLogSafetyChangeByPage(int uid, int club_id, int page, int pageLength,
			Date begin, Date end) {
		PageBean<LogSafetyChange> pageBean=(PageBean<LogSafetyChange>) redisUtil.hget(RedisConsts.HOT_LOG_SAFETYCHANGE_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime());
		if(pageBean==null) {
			int totalCount=logSafetyChangeMapper.totalCount(uid,club_id,begin,end);
			int start=page*pageLength;
			List<LogSafetyChange> list=logSafetyChangeMapper.findLogSafetyChangeByPage(uid,club_id,start,pageLength,begin,end);
			pageBean = new PageBean<>(totalCount, pageLength, page, list);
			redisUtil.hset(RedisConsts.HOT_LOG_SAFETYCHANGE_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime(), pageBean,5*60);
		}
		return pageBean;
	}

	public void insertBatch(List<LogSafetyChange> logSafetyChanges) {
		logSafetyChangeMapper.insertBatch(logSafetyChanges);
		List<String> list=new ArrayList<String>();
		for(LogSafetyChange logSafetyChange:logSafetyChanges) {
			int club_id=logSafetyChange.getClub_id();
			int uid=logSafetyChange.getUid();
			String str=club_id+"_"+uid;
			if(!list.contains(str)) {
				list.add(str);
			}
		}
		for(String str:list) {
			redisUtil.del(RedisConsts.HOT_LOG_SAFETYCHANGE_PAGE+"_"+str);
		}
	}
}
